﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GlobalObjects;

namespace DataAccessLayer
{
    public class SensorAccessTimeEntity
    {
        /// <summary>
        /// returns a list of all the reocrds in the table
        /// </summary>
        /// <returns>list of SensorAccesstime objects</returns>
        public List<SensorAccessTime> getAllSensors_DAL() {
            List<SensorAccessTime> list = new List<SensorAccessTime>();
            using (var context = new MSR2DBEntities())
            {
                var query = from s in context.sensor_access_time
                            select s;
                if (query.Count() > 0)
                {
                    foreach (var s in query)
                    {
                        SensorAccessTime sat = new SensorAccessTime(s.sensor_id, s.last_access, s.ip, s.fault_tolerance);
                        list.Add(sat);
                    }
                }
            }
            return list;

        }
        /// <summary>
        /// sets the fault tolerance of the given sensor to the new one
        /// </summary>
        /// <param name="sensorId"> sensor id</param>
        /// <param name="newT">new fault tolerance</param>
        public void setFaultTolerance_DAL(int sensorId, int newT)
        {
            using (var context = new MSR2DBEntities())
            {
                var query = from s in context.sensor_access_time
                            where s.sensor_id == sensorId
                            select s;
                if (query.Count() == 1)
                {
                    var senX = query.First();
                    senX.fault_tolerance = newT;
                    context.SaveChanges();
                }
            }
        }
        /// <summary>
        /// updates the ip address of the given sensor
        /// </summary>
        /// <param name="sensorId">sensor id</param>
        /// <param name="newIp">new ip address</param>
        public void setIp_DAL(int sensorId, string newIp)
        {
            using (var context = new MSR2DBEntities())
            {
                var query = from s in context.sensor_access_time
                            where s.sensor_id == sensorId
                            select s;
                if (query.Count() == 1)
                {
                    var senX = query.First();
                    senX.ip = newIp;
                    context.SaveChanges();
                }
            }
        }

        /// <summary>
        /// inserts or updates the last access time of a particular sensor
        /// </summary>
        /// <param name="sensorId">sensor id</param>
        /// <param name="time">last access time (UTC)</param>
        public void insertOrUpdateAcceessTime_DAL(int sensorId, string time) {
            using (var context = new MSR2DBEntities())
            {
                var query = from s in context.sensor_access_time
                            where s.sensor_id == sensorId
                            select s;
                if (query.Count() == 0)
                {
                    var lastAccess = new sensor_access_time
                        {
                            sensor_id = sensorId,
                            last_access = time,
                            fault_tolerance = 300,
                            ip = "0.0.0.0"
                        };
                    try
                    {
                        context.sensor_access_time.Add(lastAccess);
                        context.SaveChanges();
                    }
                    catch (InvalidOperationException ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    var senX = query.First();
                    senX.last_access = time;
                    context.SaveChanges();
                }
            }
        }
        
        }

    }
